package com.hd.service.solr.impl;

import com.hd.dao.BaseDao;
import com.hd.entity.solr.Products;
import com.hd.service.solr.ProductsSolrService;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * solr查询
 */
@SuppressWarnings("unchecked")
@Service("solrService")
public class ProductsSolrServiceImpl implements ProductsSolrService {
    private static final String baseURL="http://localhost:8983/solr/doc";
    @Autowired
    private BaseDao<Products,String> productsDao;
    @Override
    public List<Products> query(String keywork) throws Exception{
        HttpSolrClient httpSolrClient = new HttpSolrClient.Builder(baseURL).build();
        SolrQuery query=new SolrQuery();
        String  queryText="name:"+keywork;
        query.setQuery(queryText);
        //query分页默认是start=0,rows=10
        QueryResponse response = httpSolrClient.query(query);
       SolrDocumentList solrDocuments = response.getResults();
        System.out.println(solrDocuments.getNumFound());
        for (SolrDocument solrDocument : solrDocuments) {
            System.out.println(solrDocument.get("id"));
            System.out.println(((List<String>)solrDocument.get("name")).get(0));
            System.out.println(((List<Double>)solrDocument.get("price")).get(0));
            System.out.println(((List<String>)solrDocument.get("description")).get(0));
        }
        List<Products>vos=new ArrayList<Products>();
        return vos;
    }



    @Override
    public void save(Products products) {
        String id= UUID.randomUUID().toString();
        products.setId(id);
        productsDao.save(products);
    }

    @Override
    public void importSolrDataSource() throws Exception{
        HttpSolrClient httpSolrClient = new HttpSolrClient.Builder(baseURL).build();
        String hql="from Products";
        List<Products>productsList = productsDao.find(hql);
        if(productsList.size()>0){
            for (int i = 0; i < productsList.size(); i++) {
                SolrInputDocument document = new SolrInputDocument();
                document.addField("id",productsList.get(i).getId());
                document.addField("name",productsList.get(i).getName());
                document.addField("price",productsList.get(i).getPrice());
                document.addField("description",productsList.get(i).getDescription());
                httpSolrClient.add(document);
            }
            httpSolrClient.commit();
        }
    }
}
